package com.zysdev.videochart

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.Button

class MainActivity : AppCompatActivity(), SocketLive.SocketCallback {

    private var removeSurfaceView: SurfaceView? = null
    private var localSurfaceView: LocalSurfaceView? = null

    var decodecPlayerLiveH265: DecodecPlayerLiveH265? = null
    private var surface: Surface? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        removeSurfaceView = findViewById(R.id.removeSurfaceView)
        localSurfaceView = findViewById(R.id.localSurfaceView)

        findViewById<Button>(R.id.btn_connect).setOnClickListener {
            connect()
        }
        removeSurfaceView!!.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {

                surface = holder.surface
                decodecPlayerLiveH265 = DecodecPlayerLiveH265()
                decodecPlayerLiveH265!!.initDecode(surface!!)
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {
            }

        })
    }

    private fun connect() {
        localSurfaceView?.startLive(this)

    }

    //   socket 接收到了另外一端发来的数据
    override fun callBack(data: ByteArray?) {
        decodecPlayerLiveH265?.callBack(data)
    }
}